from core import PeaceType;
from core import PlayerColor;
from core.PeacePosition import PeacePosition;
class BoardData:

    def __init__(self):
        self._matrix = [[11, 9, 10, 14, 13, 12, 9, 10, 11],
                        [8, 8, 8, 8, 8, 8, 8, 8, 8],
				        [0, 0, 0, 0, 0, 0, 0, 0, 0],
						[0, 0, 0, 0, 0, 0, 0, 0, 0],
						[0, 0, 0, 0, 0, 0, 0, 0, 0],
						[0, 0, 0, 0, 0, 0, 0, 0, 0],
						[0, 0, 0, 0, 0, 0, 0, 0, 0],
						[1, 1, 1, 1, 1, 1, 1, 1, 1],
						[4, 3, 2, 7, 6, 5, 3, 2, 4]];
        self._commandStack = list();


    @property
    def matrix(self):
        return self._matrix;

    def getPeaceByCell(self, cell):
        return self._matrix[cell.y][cell.x];

    def getPeaceByColor(self, color):
        if(color == PlayerColor.WHITE):
            peaces = PeaceType.WHITE_PEACES;
        else:
            peaces = PeaceType.BLACK_PEACES
        result = list();
        countY = 0;
        for row in self._matrix:
            countX = 0;
            for cell in row:
                if cell in peaces:
                    result.append(PeacePosition(countX, countY));
                countX = countX + 1;
            countY = countY + 1;
        return result;

    def getPeaceByType(self, peaceType):
        result = list();
        countY = 0;
        for row in self._matrix:
            countX = 0;
            for cell in row:
                if(peaceType == cell):
                    result.append(PeacePosition(countX, countY));
                countX = countX + 1;
            countY = countY + 1;
        return result;

    def changePeace(self, cell, peaceType):
        self.matrix[cell.y][cell.x] = peaceType;
        return cell, peaceType;

    def testCommand(self, command):
        self._commandStack.append(command);
        command.execute();

    def rollBack(self):
        self._commandStack.pop().undo();

    def rollBackAll(self):
        while(len(self._commandStack)):
            self.rollBack();

    def movePeace(self, sP, fP):
        self.rollBackAll();
        self.matrix[fP.y][fP.x] = self.matrix[sP.y][sP.x]
        self.matrix[sP.y][sP.x] = 0;